package adminapi

import (
	"go-middle/app/admin/service/adminservice"
	"go-middle/app/admin/service/loginservice"
	"go-middle/app/common/model/admin"
	"go-middle/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

// Info     用户信息
// @Tags    登录
// @Summary 用户信息
// @Accept  multipart/form-data
// @Produce  json
// @Success 200 {object} response.JsonResponse "code 0成功1失败"
// @Router /api/admin/info [get]
// @Security ApiKeyAuth
func Info(r *ghttp.Request) {
	var info admin.RespUserInfo
	err := r.Session.GetStruct(loginservice.AdminInfo, info)
	if err != nil {
		glog.Error(err)
		response.Error(r, "用户信息获取失败", nil)
	}
	response.Success(r, "success", info)
}

// ChangePwd  修改密码
// @Tags    登录
// @Summary 修改密码
// @Accept   json
// @Produce  json
// @Param body body admin.ReqChangePwd true "修改密码"
// @Success 200 {object} response.JsonResponse "code 0成功1失败"
// @Router /api/admin/change_pwd [post]
// @Security ApiKeyAuth
func ChangePwd(r *ghttp.Request) {
	var req admin.ReqChangePwd
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.Error(r, err.Error(), nil)
	}
	var user admin.Entity
	err := r.Session.GetStruct(loginservice.AdminInfo, &user)
	if err != nil {
		glog.Error(err)
		response.Error(r, "获取用户失败", nil)
	}
	err = adminservice.ChangePwd(user, req)
	if err != nil {
		response.Error(r, err.Error(), nil)
	}
	response.Success(r, "操作成功", nil)
}
